﻿

namespace Lesson8__Dictionary;

class Program
{
    static void Main(string[] args)
    {
        #region 知识点一：Dictionary的基本概念
        //可以将Dictionary理解为拥有泛型的哈希表
        //它也是基于键的哈希代码 组织起来的 键/值对
        //用处：可以将原先的键 值 的object 转换为用户自定义的类型
        #endregion
        #region  知识点二：Dictionary的申明
        
        //Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
        Dictionary<int, string> dictionary = new Dictionary<int, string>();   //Dictionaty 键类型为int 值类型为string
        #endregion
        
        #region 知识点三：增添查改
        //与哈希表增添查改类似 
        
        #region   增

        dictionary.Add(1, "张三");
        dictionary.Add(2, "李四");
        dictionary.Add(3, "王五");
      
        #endregion
        
        #region    删
        
        dictionary.Remove(1);  
        dictionary.Remove(2); 
        Console.WriteLine(dictionary.Count);
        Console.WriteLine(dictionary[3]);
        dictionary.Clear();

        #endregion   
        #region 查 
        dictionary.Add(1, "张三");
        dictionary.Add(2, "李四");
        dictionary.Add(3, "王五");

        Console.WriteLine(dictionary.Count);
        Console.WriteLine(dictionary[2]);
        #endregion

        #region 改

        Console.WriteLine("***************");
        dictionary[2] = "王麻子";
        Console.WriteLine(dictionary[2]);

        #endregion
        #region 遍历
        //法一：根据键进行遍历
        foreach (int item in dictionary.Keys)

        {
            Console.WriteLine(item);    //打印键
            Console.WriteLine(dictionary[item]);  //打印键所对应的值
        }
        
        //法二：直接根据值进行遍历打印
        foreach (string item in dictionary.Values)
        {
            Console.WriteLine(item);   //直接打印值
        }
            
        //法三 新学的KeyValuePair数据类型
        foreach (KeyValuePair<int, string> item in dictionary)    //键值对
        {
            Console.WriteLine("键：" + item.Key + "\t" + "值:" + item.Value + "\t");
            
        }
            
        #endregion

        #endregion
    }
}